/* ***************************************************************************
 **   delay.c
 **
 **  Project      : ClockMatrix_v0
 **  Target       : PIC18F14K50
 **  Description  : Project used to develop clock firmware.  Tested methods and
 **                 determined functionality (needed, wanted, and the possible)
 **
 **  Compiler     : HI-TECH C  Version 9.80
 **  IDE          : MPLABX v1.80
 **  Created      : 5/8/2013
 **  Branched     : 6/27/2013 -- started v2
 **
 **  R Hanlen <implod@gmail.com>
 ** ***************************************************************************/


#ifndef _DELAY_C
#define _DELAY_C

/*****************************************************************************
 **Files to Include
 *****************************************************************************/
#include <htc.h>
#include "delay.h"

/*****************************************************************************
 ** Global Variables
 *****************************************************************************/

/*****************************************************************************
 **Delay Functions
 *****************************************************************************/
void
Delay10TCYx(unsigned char unit){
    do {
        _delay(10);
    } while(--unit != 0);
}

void
Delay100TCYx(unsigned char unit){
    do {
        _delay(100);
    } while(--unit != 0);
}

void
Delay10KTCYx(unsigned char unit){
    do {
        _delay(10000);
    } while(--unit != 0);
}

void DelayFor12TCY(void){
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
    Nop();
}

// Delay specified amount of microseconds

void DelayUs(int us){
    while(us != 0) {
        DelayFor12TCY();
        us--;
    }
}

void DelayMs(int ms){
    while(ms != 0) {
        // delay 12,000 instructions
        // due to timer0 and timer2 interrupts
        // delay runs roughly 0.73 times slower
        // so the actual delay cycles are discounted
        Delay100TCYx(87);
        Delay10TCYx(4);
        ms--;
    }
}

void DelayS(int sec){
    while(sec != 0) {
        // delay 12,000,000 instructions
        // due to timer0 and timer2 interrupts
        // delay runs roughly 0.73 times slower
        // so the actual delay cycles are discounted
        Delay10KTCYx(0); // delay 2,560,000 instructions
        Delay10KTCYx(0);
        Delay10KTCYx(0);
        Delay10KTCYx(106);
        sec--;
    }
}



#endif //_DELAY_C


